﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：李弼翀
    //
    // 文件名：DrugLibList.cs
    // 文件功能描述：产品库查询
    // 创建标识：李弼翀2009-03-30
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using HZ.DrugTrade.Model.Query;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Admin.DrugLibManage
{
    public partial class DrugLibList : BasePage
    {
        IBLL_ProcurementBasic ipb = Factory.BLL_ProcurementBasic();
        public DrugLibList()
        {
            this.ModuleNo = Module.配送关系管理.ToString();
            this.PageOperateType = OperateType.医疗机构配送关系.ToString();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                //绑定所有项目
                //IList<ProcurementProject> projectList = ipb.GetAllProcurementProject();
                //ddlProject.DataSource = projectList;
                //ddlProject.DataTextField = "ProjectName";
                //ddlProject.DataValueField = "ProjectID";
                //ddlProject.DataBind();
                //ListItem item = new ListItem();
                //item.Text = Constants.ALL_ITEM_TEXT;
                //item.Value = Constants.ALL_ITEM_VALUE;
                //ddlProject.Items.Insert(0, item);
                StoreCondition();
                //默认不显示
                //setBind();
            }
            //辅助显示
            this.txtHospitalName.Text = this.hdnHospitalName.Value;
        }

        /// <summary>
        /// 执行查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.hdnHospitalID.Value))
            {
                HZ.Web.WebUtil.Alert("请先选择要查询的医疗机构。",this);
                return;
            }
            AspNetPager1.CurrentPageIndex = 1;
            StoreCondition();
            setBind();
        }

        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            setBind();
        }

        /// <summary>
        /// 定义绑定数据的方法
        /// </summary>
        private void setBind()
        {
            try
            {
                //获取查询条件，执行查询
                IBLL_HospitalProcureCatalog ipb = Factory.BLL_HospitalProcureCatalog();
                //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
                int totalRecord = 0;
                HZ.DrugTrade.Model.Query.HospitalProcureCatalog pc = (HZ.DrugTrade.Model.Query.HospitalProcureCatalog)ViewState["v_HospitalProcureCatalog"];          
                this.gvwProduceCatalog.DataSource = ipb.GetHospitalProcureCatalog(pc, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalRecord);
                AspNetPager1.RecordCount = totalRecord;
                this.gvwProduceCatalog.DataBind();
            }
            catch (Exception ex)
            {
                PageError("数据连接失败！" + ex.Message, "");
            }
        }

        /// <summary>
        /// 存储查询条件
        /// </summary>
        private void StoreCondition()
        {
            Model.Query.HospitalProcureCatalog aProcureCatalog = new Model.Query.HospitalProcureCatalog();
            aProcureCatalog.HospitalID = hdnHospitalID.Value;
            aProcureCatalog.Companyname_Sc = txtCompanyname_Sc.Text.Trim();
     
            aProcureCatalog.Outlookc = txtOutLook.Text.Trim();
          
            aProcureCatalog.Hospital_IsUsing = true;
            //if (!string.IsNullOrEmpty(txtProcurePrice.Text.Trim()))
            //{
            //    aProcureCatalog.ProcurePrice = Convert.ToDouble(txtProcurePrice.Text.Trim());
            //}
            aProcureCatalog.GoodsName = txtProductName.Text.Trim();
            aProcureCatalog.PinyinCode = this.txtpinyincode.Text.Trim().ToUpper();
            aProcureCatalog.ProjectID = this.ddlProjectid.SelectedValue;
            aProcureCatalog.SortName = txtSortName.Text.Trim();
            if (ucSource1.SelectValue != -1) aProcureCatalog.SortSource = Convert.ToInt32(ucSource1.SelectValue);//产品属性
            aProcureCatalog.SortFields = " goodsid asc ";
            ViewState["v_HospitalProcureCatalog"] = aProcureCatalog;
        }

        /// <summary>
        /// 简单查询与高级查询的转换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// 
        protected void lbnSwitch_Click(object sender, EventArgs e)
        {
            if (lbnSwitch.CommandArgument == "simple")
            {
                ltlAdvance.Text = "";
                lbnSwitch.CommandArgument = "advance";
                lbnSwitch.Text = "简单查询";
            }
            else
            {
                ltlAdvance.Text = "<tbody id='advance' style='display:none'>";
                lbnSwitch.CommandArgument = "simple";
                lbnSwitch.Text = "高级查询";

                //清空数据
                txtCompanyname_Sc.Text = "";
          
                txtHospitalName.Text = "";
              
                //txtProcurePrice.Text = "";
                txtSortName.Text = "";

                ddlProjectid.SelectedIndex = -1;
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            IBLL_HospitalProcureCatalog ipb = Factory.BLL_HospitalProcureCatalog();
            int totalRecord = 0;
            HZ.DrugTrade.Model.Query.HospitalProcureCatalog pc = (HZ.DrugTrade.Model.Query.HospitalProcureCatalog)ViewState["v_HospitalProcureCatalog"];
            IList<HZ.DrugTrade.Model.View.HospitalProcureCatalog>  hplist = ipb.GetHospitalProcureCatalog(pc, 1, 0, out totalRecord);
            System.Data.DataTable tb = HZ.Data.ModelConvert.GetDataTableFromModel(hplist);
            HZ.Excel.ExcelHelper.ExportDataForWeb(tb, gvwProduceCatalog);
        }

        protected void gvwProduceCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                if (ddlProjectid.SelectedItem.Value == "0002")
                {
                    e.Row.Cells[9].Text = "自主报价";
                    gvwProduceCatalog.Columns[10].Visible = false;
                }
                else
                {
                    e.Row.Cells[9].Text = "采购限价";
                    gvwProduceCatalog.Columns[10].Visible = true;
                }
                gvwProduceCatalog.Columns[8].Visible = false;
            }
        }
    }
}
